<template>
  <div class="data-container">
    <div class="tab-panel">
      <div class="slot">
        <slot></slot>
      </div>
      <p class="text">{{ title }}</p>
      <div :id="id" class="echarts"></div>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'panel1',
  props: {
    title: { type: String, default: '' },
    data: { type: Object, default: () => {} },
    id: { type: String, default: '' },
  },
  data() {
    return {
      myEcharts: '',
      option: {},
    }
  },
  watch: {
    data: {
      deep: true,
      handler(val) {
        if (val && val.key && val.key.length) {
          this.initEcharts()
        }
      },
    },
  },
  mounted() {
    this.initEcharts()
  },
  methods: {
    // 初始化图表
    initEcharts() {
      let echartsData = []
      this.data.key.forEach((item, index) => {
        if (index < 20) {
          echartsData.push({ name: item, value: this.data.val[index] })
        }
      })

      this.myEcharts = echarts.init(document.getElementById(this.id))
      this.option = {
        tooltip: { trigger: 'item' },
        series: [
          {
            type: 'pie',
            radius: '50%',
            center: ['50%', '50%'],
            color: ['#68cee0', '#54a7b7', '#408291', '#2c5a6b', '#eedd78', '#87804a', '#6a643c', '#5b5736'],
            label: {
              show: true,
              formatter: '{b}: {c}',
            }, // 隐藏标签文字，可根据需要设置为 true 显示标签文字
            data: echartsData,
          },
        ],
      }

      // 使用刚指定的配置项和数据显示图表。
      this.myEcharts.setOption(this.option)
      this.myEcharts.on('click', (param) => {
        this.$emit('click', param)
      })
    },
    onResize() {
      if (this.myEcharts) {
        this.myEcharts.resize()
      }
    },
  },
}
</script>
<style lang="scss" scoped>
$bgColor: #060a22;
$mainColor: #68cee0;
$mainColorOpcity: rgba(104, 206, 224, 0.3);
.data-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  padding: 20px;
  overflow: hidden;
}
.echarts {
  width: 100%;
  height: 100%;
}
.tab-panel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;
  position: relative;
  .text {
    width: 100%;
    color: #ffffff;
  }
}

.slot {
  position: absolute;
  top: 4px;
  right: 0;
}
</style>
